package com.hp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hp.entity.Position;
import com.hp.service.PositionService;
import com.hp.service.impl.PositionServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * 职称表
 */
@Controller
@RequestMapping("/position")
public class PositionController  {

	@Autowired
	private PositionServiceImpl positionService;

	@RequestMapping("/listPage")
	public String listPage(Model model, @RequestParam(value = "current",defaultValue = "1") Integer current, @RequestParam(value = "size",defaultValue = "3") Integer size){
		Page<Position>  positionPage = positionService.page(new Page<>(current, size), new QueryWrapper<Position>().orderByDesc("position_number"));
		List<Position> records = positionPage.getRecords();
		model.addAttribute("positionList",records);//返回的集合
		model.addAttribute("current",current);//当前页数
		model.addAttribute("positionPage",positionPage);//操作页方法
		return  "position/position_list";
	}
	@RequestMapping("/toadd")
	public String toadd(Model model){
		 /*
		 * 两种情况,
		 * 1.数据库无数据,直接给个初始的部门编号
		 * 2.数据库有数据,需要从数据库从查出最大的部门编号
		 * */
		Integer maxNum= positionService.selectMaxNum();
		//如果部门数据等于null
		if (maxNum!=null){
			//查询部门最大编号
//			QueryWrapper<Position> queryWrapper = new QueryWrapper<>();
//			queryWrapper.orderByDesc("positionNumber");
			System.out.println(maxNum);
			model.addAttribute("maxNum",maxNum+1);
		}else{
			//如果为空,返回默认值
			model.addAttribute("maxNum","2001");
		}
		return "position/position_add";
	}
	//添加方法
	@RequestMapping("/addPosition")
	public String addDepartment(Position position,Model model){
		boolean save = positionService.save(position);
		 return save?"redirect:/position/listPage":"redirect:/position/toadd";
	}
	//跳转到修改页面
	@RequestMapping("/toUpdate/{id}")
	public String toUpdate(Model model, @PathVariable("id") Integer id){
		System.out.println(id);
		Position position = positionService.getById(id);
		model.addAttribute("position", position);
		return "position/position_update";
	}
//	//修改部门信息方法
	@RequestMapping("/updPosition")
	public String updPosition(Model model,Position position){
		System.out.println(position);
		boolean b = positionService.updateById(position);
		return b?"redirect:/position/listPage":"position/position_update";
	}

	//删除部门信息的方法
	@RequestMapping("/delPosition/{id}")
	public String delPosition(Model model, @PathVariable String id){
		boolean b = positionService.removeById(id);
		if (b){
			model.addAttribute("err","删除成功");
			return "redirect:/position/listPage";
		}else {
			model.addAttribute("err","删除失败");
			return "redirect:/position/listPage";
		}
	}
}
